package com.itsm.controller;

import com.alibaba.fastjson.JSONObject;
import com.itsm.entity.*;
import com.itsm.entity.Process;
import com.itsm.service.ApproveService;
import com.itsm.service.ProcessAllService;
import com.itsm.service.SendService;
import com.itsm.service.UserService;
import com.itsm.service.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class ProcessAllController {
    @Autowired
    private ProcessAllService processService;

    @Autowired
    private SendService sendService;

    @Autowired
    private UserService userService;
    @Autowired
    private ApproveService approveService;


    /**
     * 插入工程师姓名,发送IT服务信息
     * @param process 表单信息
     * @return
     */
    @PostMapping("updateProcess")
    public Integer updateEn(@RequestBody Process process) throws IOException {
        List<Approve> approve = approveService.get3(process.getProcessNo());
        if(process.getIsDelete()!=0){
            if(approve.size() != 0){
                Approve approve1 = approve.get(0);
                Approve approve2 = approve.get(1);
                if(approve1.getStatus()==2 || approve2.getStatus()==2){
                    approve1.setSubTime(new Date());
                    approve1.setApproveTime(null);
                    approve1.setStatus(0);
                    approve1.setId(null);
                    approve1.setCount(approve1.getCount()+1);
                    sendService.sendSup(process,approve1.getApprover());
                    approveService.addApprove(approve1);
                    approve2.setId(null);
                    approve2.setApproveTime(null);
                    approve2.setStatus(0);
                    approve2.setSubTime(null);
                    approve2.setCount(approve2.getCount()+1);
                    approveService.addApprove(approve2);
                }
                if(approve.size()==3){
                    Approve approve3 = approve.get(2);
                    if( approve3.getStatus()==2){
                        approve1.setSubTime(new Date());
                        approve1.setApproveTime(null);
                        approve1.setStatus(0);
                        approve1.setId(null);
                        approve1.setCount(approve1.getCount()+1);
                        approveService.addApprove(approve1);
                        approve2.setId(null);
                        approve2.setApproveTime(null);
                        approve2.setStatus(0);
                        approve2.setSubTime(null);
                        approve2.setCount(approve2.getCount()+1);
                        approveService.addApprove(approve2);
                    }
                }

            }
        }


        if (process.getState()==2 && process.getScore()==null){
            process.setOktime(new Date());
            sendService.sendOver(process);
        }
        if(process.getState()==1&&process.getIsAccepted()==1){
            process.setAcceptTime(new Date());
            sendService.sendAccepted(process);
        }
        if(process.getState()==1&&process.getIsAccepted()==0){
            sendService.sendEn(process);
        }
        if(process.getState()==2&&process.getScore()!=null){
            sendService.sendRate(process);
        }

//        if(process.getFileList().equals( processService.select(process.getProcessNo()).getFileList())){
//            return processService.updateEn(process);
//        }else {
//            List<String> listName = new ArrayList<>();
//            List<FileName> fileNames = JSONObject.parseArray(JSONObject.toJSONString(process.getFileList()), FileName.class);
//            for (FileName fileName : fileNames) {
//                listName.add(fileName.getName());
//            }
//            process.setFileList(StringUtils.strip(listName.toString(),"[]"));
//        }
        return processService.updateEn(process);

    }





    /**
     * 查询各状态的工单
     * @return 工单信息
     */
    @GetMapping("assignProcess")
    public List<Process> assignProcess(Integer state, Integer is,HttpServletRequest request){
        String userid = (String) request.getSession().getAttribute("userid");
        User user = userService.selectUser(userid);
        //判断权限是否有两厂的
        if(user.getPlantAll()!=1){
            return processService.selectProcessAcc1(state,is,user.getPlant());
        }
        return processService.selectProcessAcc(state,is);
    }

    /**
     * 查询未审批的工单
     * @return 工单信息
     */
    @GetMapping("assignProcess1")
    public List<Process1> assignProcess1(HttpServletRequest request){
        String userid = (String) request.getSession().getAttribute("userid");
        User user = userService.selectUser(userid);
        return processService.selectListUnApp(user.getPlant(),user.getPlantAll());

    }


    /**
     * 查询未派单的工单
     * @return 工单信息
     */
    @GetMapping("assignProcess2")
    public List<Process> assignProcess2(HttpServletRequest request){
        String userid = (String) request.getSession().getAttribute("userid");
        User user = userService.selectUser(userid);
        return processService.UnDispatch(user.getPlant(),user.getPlantAll());

    }

    /**
     * 查询所有未评分的工单
     * @param state 状态,0创建，1转派，3完成
     * @param is 0未接单，1接单
     * @return 所有未评分的工单
     */
    @GetMapping("assignRateNo")
    public List<Process> assignRateNo(Integer state,Integer is){
        return processService.selectRateNo(state,is);
    }


    @GetMapping("assignRate")
    public List<Process> assignRate(Integer state,Integer is){
        return processService.selectRate(state,is);
    }
//    /**
//     *指派人工单查询（查询全部）
//     * @return 全部工单
//     */
//    @GetMapping("/assign_query")
//    public List<Process> assignQuery() {
//        List<Process> list = processService.selectListAssign ();
//        return list;
//    }

    /**
     *指派人工单查询（查询区分的全部工单）
     * @return 全部工单
     */
    @GetMapping("/assign_query")
    public List<Process1> assignQuery(HttpServletRequest request) {
        String userid = (String) request.getSession().getAttribute("userid");
        User user = userService.selectUser(userid);
        return processService.selectListAssign1(user.getPlant(),user.getPlantAll());
    }

    /**
     * 筛选工单查询（服务台）
     * @param request
     * @return
     */
    @GetMapping("/assign_query1")
    public List<Process1> assignQuery1(HttpServletRequest request,String userName) {
        String userid = (String) request.getSession().getAttribute("userid");
        User user = userService.selectUser(userid);
        return processService.selectListAssign2(user.getPlant(),user.getPlantAll(),userName);
    }

}
